package com.example.mcdonaldapplication.activity

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Looper
import android.util.Log
import android.widget.Toast
import com.example.mcdonaldapplication.databinding.ActivityChargeBinding
import com.example.mcdonaldapplication.info.ServerIP
import com.example.mcdonaldapplication.info.UserInfo
import okhttp3.FormBody
import okhttp3.OkHttpClient
import okhttp3.Request
import org.json.JSONObject
import kotlin.concurrent.thread

class ChargeActivity : AppCompatActivity() {
    private lateinit var info:UserInfo
    private lateinit var binding: ActivityChargeBinding
    private val serverIp = ServerIP.getIp()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityChargeBinding.inflate(layoutInflater)
        info = UserInfo(0, "test", "test","test","test","test",0.0,"test", ArrayList(1),ArrayList(1))
        setContentView(binding.root)
        supportActionBar?.hide()

        getUser()
        val btnSaveSE = binding.btnSave
        val editSave = binding.editSave
        val btnDel = binding.btnDel
        btnSaveSE.setOnClickListener{
            if(editSave.text.toString() != ""){
                val balance = editSave.text.toString()
                thread {
                    charge(info.uid, balance)
                }
                Toast.makeText(this, "充值成功", Toast.LENGTH_SHORT).show()
//                finish()
            }else{
                Toast.makeText(this, "充值失败,请输入金额", Toast.LENGTH_SHORT).show()
            }
        }

        btnDel.setOnClickListener{
            editSave.setText("")
        }
    }

    private fun charge(uid:Int, balance:String) {
        thread {
            Log.d("uid", uid.toString())

            //post方式提交的数据
            val formBody: FormBody = FormBody.Builder()
                .add("uid", uid.toString())
                .add("money", balance)
                .build()
            val client = OkHttpClient()
            val request = Request.Builder()
                .url("http://${serverIp}/user/rechange")
                .post(formBody)
                .build()
            val response = client.newCall(request).execute()
            val responseData = response.body.string()
            parseJson(responseData, balance)
        }
    }

    private fun parseJson(jsonData: String, balance:String) {
        try {
            val jsonObject = JSONObject(jsonData)
            val success = jsonObject.optString("success")

            if(success == "true") {
                val money = balance.toDouble() + info.balance.toDouble()
                val editor = getSharedPreferences("user", Context.MODE_PRIVATE).edit()
                editor.putString("balance", money.toString())
                editor.apply()
                finish()
            }else{
                Looper.prepare()
                Toast.makeText(this, "充值失败", Toast.LENGTH_SHORT).show()
                Looper.loop()
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    private fun getUser(){
        val prefs = getSharedPreferences("user", MODE_PRIVATE)
        val uid = prefs.getInt("uid", 0)
        val balance = prefs.getString("balance", "0")
        info.uid = uid
        if (balance != null) {
            info.balance = balance
        }
    }

}